import numpy as np

def tanh(x):
    return np.tanh(x)

def tanh_derivative(x):
    return 1 - tanh(x) * tanh(x)

def logistic(x):
    return 1 / (1 + np.exp(-x))

def logistic_derivative(x):
    return logistic(x) * (1 - logistic(x))

class NeuralNetwork:
    def __init__(self,layers,activation='tanh'):
        if activation == 'logistic':
            self.activation = logistic
            self.activation_derivative = logistic_derivative
        elif activation == 'tanh':
            self.activation = tanh
            self.activation_derivative = tanh_derivative

        #获得用户输入的层数
        self.layer_number = len(layers)
        #获得用户输入的节点总数，用于确定矩阵
        self.node_number = sum(layers)
        #weight_matrix是一个二维数组，对角线元素存节点的阈值，其他元素存权重
        self.weight_matrix = np.random.randn(self.node_number,self.node_number)
        #保留其上三角部分
        self.weight_matrix = np.triu(self.weight_matrix)
        #将上三角拷贝到下三角
        self.weight_matrix += self.weight_matrix.T - np.diag(self.weight_matrix.diagonal())

    def get_weight_matrix(self):
        return self.weight_matrix

